package com.td.client.gui;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;

import mdes.slick.sui.Display;
import mdes.slick.sui.Label;
import mdes.slick.sui.ToggleButton;
import mdes.slick.sui.Window;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;
import mdes.slick.sui.event.MouseEvent;
import mdes.slick.sui.event.MouseListener;

import org.newdawn.slick.Color;
import org.newdawn.slick.gui.GUIContext;

import com.td.client.states.GameplayState;
import com.td.core.communication.obj.GameStateInfo.STATE;
import com.td.core.globals.ObjectsGlobal.TowerType;
import com.td.core.globals.TDGlobals;
import com.td.core.messages.TDMessage;
import com.td.core.obj.Player;
import com.td.core.obj.tower.Tower;
import com.td.core.obj.tower.Tower1;
import com.td.core.obj.tower.Tower2;
import com.td.core.obj.tower.Tower3;
import com.td.core.obj.tower.Tower4;
import com.td.core.obj.tower.Tower5;

public class Frame extends Display{

	
	private ArrayList<ToggleButton> towersButtons;
	private GameplayState gameplay;
	private Player player;
	private Label moneyLabel;
	private Label pointLabel;
	private Label passedUnitsLabel;
	private Label messageLabel;
	
	Window messageWindow;
	
	Window towerInfoWindow;
	
	private boolean mouseOnFrame;
		
	public Frame(GUIContext context, GameplayState gameplay, Player player) {
		super(context);
		
		this.gameplay = gameplay;
		this.player = player;
		
		buildGUI();
	}

	private void buildGUI() {
		
		int FRAME_X = 0;
		int FRAME_Y = 600;
		int BUTTON_SIZE = 50;
		
		Window window = new Window();
		window.addMouseListener(new FrameMouseListener());
		window.setOpaque(true);
		window.setBackground(Color.lightGray);	
		window.setSize(800, 100);
		window.setLocation(FRAME_X, FRAME_Y);
		window.setAlwaysOnTop(true);
				
		int left = 20;
		int top = 25;
		
		ToggleButton simpleTowerButon = new AddTowerButton("Simple", TowerType.TOWER1);
		simpleTowerButon.setLocation(left, top);
		simpleTowerButon.setSize(BUTTON_SIZE, BUTTON_SIZE);	
		window.add(simpleTowerButon);
		
		left += BUTTON_SIZE + 20;
		ToggleButton simpleTowerButon2 = new AddTowerButton("Simple2", TowerType.TOWER2);
		simpleTowerButon2.setLocation(left , top);
		simpleTowerButon2.setSize(BUTTON_SIZE, BUTTON_SIZE);	
		window.add(simpleTowerButon2);
		
		left += BUTTON_SIZE + 20;
		ToggleButton simpleTowerButon3 = new AddTowerButton("Simple3", TowerType.TOWER3);
		simpleTowerButon3.setLocation(left , top);
		simpleTowerButon3.setSize(BUTTON_SIZE, BUTTON_SIZE);	
		window.add(simpleTowerButon3);
		
		left += BUTTON_SIZE + 20;
		ToggleButton simpleTowerButon4 = new AddTowerButton("Simple4", TowerType.TOWER4);
		simpleTowerButon4.setLocation(left , top);
		simpleTowerButon4.setSize(BUTTON_SIZE, BUTTON_SIZE);	
		window.add(simpleTowerButon4);
		
		left += BUTTON_SIZE + 20;
		ToggleButton simpleTowerButon5 = new AddTowerButton("Simple5", TowerType.TOWER5);
		simpleTowerButon5.setLocation(left , top);
		simpleTowerButon5.setSize(BUTTON_SIZE, BUTTON_SIZE);	
		window.add(simpleTowerButon5);
		
		left += 200;
		Label moneyTextLabel = new Label("money:");
		moneyTextLabel.setLocation(left, top);
		moneyTextLabel.setSize(50, 50);
		window.add(moneyTextLabel);
		
		moneyLabel = new Label();
		moneyLabel.setLocation(left, top + 20);
		moneyLabel.setSize(50, 50);
		window.add(moneyLabel);
		
		left += 100;
		Label pointTextLabel = new Label("points:");
		pointTextLabel.setLocation(left, top);
		pointTextLabel.setSize(50, 50);
		window.add(pointTextLabel);
		
		pointLabel = new Label();
		pointLabel.setLocation(left, top + 20);
		pointLabel.setSize(50, 50);
		window.add(pointLabel);
		
		left += 100;
		Label passedUnitsTextLabel = new Label("passed units:");
		passedUnitsTextLabel.setLocation(left, top);
		passedUnitsTextLabel.setSize(50, 50);
		window.add(passedUnitsTextLabel);
		
		passedUnitsLabel = new Label();
		passedUnitsLabel.setLocation(left, top + 20);
		passedUnitsLabel.setSize(50, 50);
		window.add(passedUnitsLabel);
		
		this.add(window);		
		
		
		messageWindow = new Window();
		messageWindow.setOpaque(true);
		messageWindow.setBackground(new Color(255, 255, 255, 100));
		messageWindow.setLocation(10, 50);
		messageWindow.setAlwaysOnTop(true);
		
		messageLabel = new Label();
		messageLabel.setLocation(0, 0);
		messageLabel.setSize(100, 10);
		messageWindow.add(messageLabel);
		
		this.add(messageWindow);
		
		towerInfoWindow = new Window();
		towerInfoWindow.setOpaque(true);
		towerInfoWindow.setBackground(Color.lightGray);
		towerInfoWindow.setSize(300, 100);
		towerInfoWindow.setLocation(0, 500);
		towerInfoWindow.setAlwaysOnTop(true);
		towerInfoWindow.setVisible(false);
		this.add(towerInfoWindow);
		
	}
	
	public void myUpdate() {
		
		try {
			moneyLabel.setText(player.getGold() + "");
			moneyLabel.updateAppearance();
			
			pointLabel.setText(player.getPoints() + "");
			pointLabel.updateAppearance();
			
			passedUnitsLabel.setText(player.getPassedUnits() + "/" + TDGlobals.maxPassedUnits);
			passedUnitsLabel.updateAppearance();
			
			//messages
			
			
			int windowHeight = 0;
			messageLabel.setText("");
			String text = "";
			for(TDMessage message : player.getMessages()) {
				text += message.getText() + "\n";
				windowHeight += 20;
			}
			
			messageLabel.setText(text);
			messageLabel.updateAppearance();
			
			messageWindow.setSize(200, windowHeight);
			messageWindow.updateAppearance();
		
		} catch (ConcurrentModificationException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isOn() {
		return mouseOnFrame;
	}
	
	public void unselectButtons() {
		
		unselectButtons(towersButtons, null);
	}
	
	public void changeEnabled(STATE state) {
		
		switch(state) {
		case BUILDING:
			for(ToggleButton button: towersButtons)
				button.setEnabled(true);
			break;
		case ACTION:
			for(ToggleButton button: towersButtons)
				button.setEnabled(false);
			break;
	}
		
	}
	
	private void unselectButtons(ArrayList<ToggleButton> buttons, ToggleButton thisButton) {
		
		for(ToggleButton b : buttons)
			if(!b.equals(thisButton))
				b.setSelected(false);
	}
	
	private void addTowerProcess(TowerType towerType) {
		
		Tower tower = getTower(towerType);

		gameplay.getGameplayInfo().setNet(true);
		gameplay.getGameplayInfo().setAddingTower(tower);
	}
	

	private Tower getTower(TowerType towerType) {
		
		int x = -100;
		int y = -100;
		
		switch (towerType) {
		case TOWER1 :
			return new Tower1(x, y);
		case TOWER2 :
			return new Tower2(x, y);
		case TOWER3 :
			return new Tower3(x, y);
		case TOWER4 :
			return new Tower4(x, y);
		case TOWER5 :
			return new Tower5(x, y);
		default:
			return null;
		}
	}
	
		
	private class AddTowerButton extends ToggleButton {
		
		public AddTowerButton(String text, final TowerType towerType) {
				
			super(text);
			
			if(towersButtons == null)
				towersButtons = new ArrayList<ToggleButton>();
			
			towersButtons.add(this);
			
			addMouseListener(new TowerButtonMouseListener(towerType));
			
			addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					unselectButtons(towersButtons, getThisButton());
					
					if(isSelected()) {
						
						gameplay.setAddingTowerType(towerType);
						addTowerProcess(towerType);
					}
					else
						gameplay.setDefaultMode();
										
				}
			
			});
		
		}
		
		private ToggleButton getThisButton() {
			return this;
		}
				
	}
	
	private class FrameMouseListener implements MouseListener {

		@Override
		public void mouseDragged(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			mouseOnFrame = true;
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			mouseOnFrame = false;
		}

		@Override
		public void mouseMoved(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class TowerButtonMouseListener extends FrameMouseListener {
		
		Tower towerInfo;
		
		public TowerButtonMouseListener(TowerType towerType) {
			towerInfo = getTower(towerType);
		}
		
		@Override
		public void mouseEntered(MouseEvent arg0) {
			super.mouseEntered(arg0);
			towerInfoWindow.setVisible(true);
			Label left = new Label();
			left.setLocation(0, 0);
			left.setSize(100, 100);
			
			Label middle = new Label();
			middle.setLocation(140, 0);
			middle.setSize(100, 100);
			
			Label right = new Label();
			right.setLocation(200, 0);
			right.setSize(100, 100);
			
			String leftInfo = "";
			leftInfo += towerInfo.getName() + "\n";
			leftInfo += towerInfo.getPrice() + " gold";
			left.setText(leftInfo);
			
			right.setText(towerInfo.getName());
			
			
			String middleInfo = "";
			middleInfo += "damage: \n";
			middleInfo += "speed: \n";
			middleInfo += "range:\n";
			middleInfo += "splash:";
			
			middle.setText(middleInfo);
			
			String rightInfo = "";
			rightInfo += towerInfo.getMinDamage() + " - " + towerInfo.getMaxDamage() + "\n";
			rightInfo += towerInfo.getAttackSpeed() + "\n";
			rightInfo += towerInfo.getRange() + "\n";
			rightInfo += towerInfo.getSplash();
			
			right.setText(rightInfo);
			
			towerInfoWindow.add(left);
			towerInfoWindow.add(middle);
			towerInfoWindow.add(right);
			towerInfoWindow.updateAppearance();
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			super.mouseExited(arg0);
			towerInfoWindow.setVisible(false);
			towerInfoWindow.removeAll();
		}
				
	}
	
}
